import app, { xlsxCfgNames } from "../../../app";
import { BuffEffect, EffectCondition, EffectType, SK_type } from "../../../shared/interface";
import { logger } from "../../server/logger";
import { baseBody } from "../body/baseBody";
import body_tool from "../body/body_tool";
import { calls } from "../body/calls";
import { player, wallet_key } from "../body/player";
import buff_item from "../buff/buff_item";
import { Effect, SKILL } from "./SK_active";

class SK_applyEffect {
    constructor() {

    }
    private log(SK: SKILL, log, tag?: baseBody) {
        if (typeof (log.val) == 'number' && !log.val) {
            return;
        }
        if (log.type != EffectType.none) {
            if (tag) {
                tag.get_battle()?.addLog(log)
            } else {
                SK._parent.get_battle()?.addLog(log)
            }

        }
    }
    count(SK: SKILL, eff: Effect, tag: baseBody) {
        const skVal: any = 0;

        let haveDie = tag.is_die();
        let log = { ...SK.tool_getLogInfo(tag), val: skVal, type: EffectType.none };
        const freeUser = SK._parent;
        const free_att = freeUser._attributes;
        log.type = eff.effect;
        let buffKey;
        log.val = SK._formula(eff, tag);
        switch (eff.effect) {
            case EffectType.对目标造成物理伤害:
                // 附加暴击伤害
                body_tool.cover_Physical_crit(freeUser, log)

                // 扣除白盾
                let ph_wallLog = { ...log }
                body_tool.del_wall_hp(tag, ph_wallLog)
                this.log(SK, ph_wallLog, tag)

                log.val -= ph_wallLog.val;
                // 进行伤害
                body_tool.hit_Physical(tag, log, SK);
                // 附加物理吸血
                let _ph_lsLog = { ...SK.tool_getLogInfo(tag), val: (tag._attributes.Physical_Ls / 100) * log.val, type: EffectType.恢复目标生命值 };
                body_tool.add_hp(tag, _ph_lsLog)
                if (_ph_lsLog.val > 0.01) {
                    this.log(SK, _ph_lsLog)
                }
                break;
            case EffectType.对目标造成魔法伤害:
                // 附加暴击伤害
                body_tool.cover_Magic_crit(freeUser, log)

                // 扣除白盾
                let _mg_wallLog = { ...log }
                body_tool.del_wall_hp(tag, _mg_wallLog)
                this.log(SK, _mg_wallLog, tag)

                log.val -= _mg_wallLog.val;
                // 进行伤害
                body_tool.hit_Magic(tag, log);
                // 附加法术吸血
                let _lsLog = { ...SK.tool_getLogInfo(tag), val: (tag._attributes.Magic_Ls / 100) * log.val, type: EffectType.恢复目标生命值 };
                body_tool.add_hp(tag, _lsLog)
                if (_lsLog.val > 0.01) {
                    this.log(SK, _lsLog)
                }
                break;
            case EffectType.屏障:
                body_tool.add_wall_hp(tag, log)
                break;
            case EffectType.恢复目标生命值:
                body_tool.add_hp(tag, log)
                break;
            case EffectType.临时提升目标力量:
                tag.addBuff(new buff_item(Date.now() + 100 * 1000, BuffEffect.力量, log.val, SK.tool_getLogInfo(tag)))
                break;
            case EffectType.永久增加最大生命值:

                tag.forever_addHpMax(log.val);
                break;
            case EffectType.沉默单体:
                tag.addBuff(new buff_item(Date.now() + log.val * 1000, BuffEffect.沉默, 1, SK.tool_getLogInfo(tag)))
                log.val = `🤐沉默${tag.nickName}${Math.ceil(log.val)}秒`;
                break;
            case EffectType.真实伤害:
                body_tool.hit_read(tag, log);
                break;
            case EffectType.增加金币:
                let master = tag.get_Master();
                if(master instanceof player){
                    master.wallet_change(wallet_key.gold,log.val)
                }
                break;
            case EffectType.召唤永久单位:
                log.type = EffectType.召唤永久单位;
                /**
                 * 1.创建指定ID 召唤单位
                 * 2.加入到友军战场
                 * 
                 */
                if (!eff.data.create_id) {
                    debugger;
                    return;
                }
                let battle = tag.get_battle();
                if (!battle) {
                    debugger;
                    return;
                }
                const table = app.xlsxCfgMap.get(xlsxCfgNames.召唤表) as Map<string, any>;
                const create_id = eff.data.create_id;
                const Calls_sore = free_att.Calls_sore;
                let leve_max = free_att.Calls_leveMax;
                let leve_min = free_att.Calls_leveMax;
                let Calls_cont = Math.floor(free_att.Calls_cont);
                if (Calls_cont == 0) {
                    log.val = `召唤失败`;
                    return;
                }
                Calls_cont = app.generateRandom(1, Math.floor(free_att.Calls_cont));
                let call_name = '';
                let call_leve = 0;
                for (let i = 0; i < Calls_cont; i++) {
                    let body = app.createUnity(create_id, app.generateRandom(leve_min, leve_max), table, app.generateRandom(Calls_sore / 5, Calls_sore * 1.2), 1) as calls;
                    body.setMaster(tag);
                    battle.join(body, tag.get_group());
                    call_name = body.name;
                    call_leve = body.leve.num;
                }
                if (Calls_cont == 1) {
                    log.val = `${call_name}-Lv.${call_leve}`;
                } else {
                    log.val = `${call_name}X${Calls_cont}`;
                }

                break;


            case EffectType.buff_提升物理吸血:
                buffKey = BuffEffect.物理吸血
                break;
            case EffectType.buff_提升生命恢复:
                buffKey = BuffEffect.生命恢复速度
                break;
            case EffectType.buff_提升最大魔法值:
                buffKey = BuffEffect.最大魔法值
                break;
            case EffectType.buff_提升魔法值恢复速度:
                buffKey = BuffEffect.魔法恢复速度
                break;
            case EffectType.buff_提升最小物攻:
                buffKey = BuffEffect.最小物攻
                break;
            case EffectType.buff_提升最大物攻:
                buffKey = BuffEffect.最大物攻
                break;
            case EffectType.buff_提升攻击速度:
                buffKey = BuffEffect.攻击速度
                break;
            case EffectType.buff_提升最大魔攻:
                buffKey = BuffEffect.最大魔攻
                break;
            case EffectType.buff_提升最小魔攻:
                buffKey = BuffEffect.最小魔攻
                break;
            case EffectType.buff_提升魔法防御:
                buffKey = BuffEffect.魔法防御
                break;
            case EffectType.buff_提升物理防御:
                buffKey = BuffEffect.物理防御
                break;
            case EffectType.buff_提升魔法暴击率:
                buffKey = BuffEffect.魔法暴击率
                break;
            case EffectType.buff_提升物理暴击率:
                buffKey = BuffEffect.物理暴击率
                break;
            case EffectType.buff_提升魔法吸血:
                buffKey = BuffEffect.魔法吸血
                break;
            case EffectType.buff_提升最大生命:
                buffKey = BuffEffect.最大生命
                break;
            default:
                break;
        }
        if (buffKey) {
            let time = 0;
            if (eff.condition == EffectCondition.auto_附带即生效) {
                time = -1;
            } else {
                if (!eff.data.time) {
                    debugger;
                    return;
                }
                time = Date.now() + eff.data.time * 1000
            }

            tag.addBuff(new buff_item(time, buffKey, log.val, SK.tool_getLogInfo(tag)))
        }
        this.log(SK, log)
        if (!haveDie && tag.is_die()) {
            SK._parent.get_battle()?.addKillLog(SK._parent, tag, SK);
            tag.sk_trigger(EffectCondition.auto_死亡时, [tag])
        }
    }
}
export default new SK_applyEffect();